# A Demo of a service that takes advantage of the additional notifications
# available in later Windows versions.

# Note that all output is written as event log entries - so you must install
# and start the service, then look at the event log for messages as events
# are generated.

# Events are generated for USB device insertion and removal, power state
# changes and hardware profile events - so try putting your computer to
# sleep and waking it, inserting a memory stick, etc then check the event log

"""
Usage : python aservice.py install
 Usage : python aservice.py start
 Usage : python aservice.py stop
 Usage : python aservice.py remove

"""

import win32serviceutil, win32service
import win32event
import servicemanager

from xmlrpc_4d import *

# Most event notification support lives around win32gui
import win32gui, win32gui_struct, win32con

class XmlrpcService(win32serviceutil.ServiceFramework):
    _svc_name_ = "xmlrpc_4d_server"
    _svc_display_name_ = "XMLRPC 4D Service"
    _svc_description_ = "Serveur de fonctions XMLRPC en liaison avec 4D"

    def __init__(self, args):
        win32serviceutil.ServiceFramework.__init__(self, args)
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)

        self.server = XMLRPC4DServer()

    def SvcStop(self):
        self.server.shutdown()
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        win32event.SetEvent(self.hWaitStop)

    def SvcDoRun(self):
        self.ReportServiceStatus(win32service.SERVICE_START_PENDING)
        self.server.serve_forever()
        # do nothing at all - just wait to be stopped
        win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
        # Write a stop message.
        servicemanager.LogMsg(
                servicemanager.EVENTLOG_INFORMATION_TYPE,
                servicemanager.PYS_SERVICE_STOPPED,
                (self._svc_name_, '')
                )

if __name__=='__main__':
    win32serviceutil.HandleCommandLine(XmlrpcService)
